
var havePick = false;

;$(function(){

    setTimeout(function(){
        $('.part-two').show();
    }, 400);

    $('.card').click(function(){
        if (havePick) {return;}
        havePick = true;
        // var index = $(this).attr('class');
        $(this).hide();
        $('.m-focus').css('opacity', 0);
        setTimeout(function(){
            $('.m-bigcard').addClass('dropout');
            $('.frame').hide();
            setTimeout(function(){
               $('.m-bigcard p').css('opacity', 1);
            },1000);
        },500);
    })

    $('#goPayment').click(function(){
        createOrder();
    })
})

// 生成订单
function createOrder() {
    var formData = LXMBase.getObjItem('tr_data'),
        uid = LXMBase.getItem('biz_user_id'),
        biz_source = LXMBase.getItem('biz_source'),
        biz_operator = LXMBase.getItem('biz_operator') || '',
        op_from = LXMBase.getItem('op_from') || '',
        user_phone = LXMBase.isPhone(uid) ? uid : '';

    if (!uid) {
        alertMsg('没有登录哦');
        return;
    }
    if (!biz_source) {
        alertMsg('没有登录哦.');
        return;
    }
    if (!formData) {
        return;
    }

    // 阻止多次提交
    if (LXMBase.data.sc_genorder) { return; }
    LXMBase.data.sc_genorder = true;

    showLoading();
    var param = {
        uid: '',
        tk: '',
        cancellation: -1,
        body: {
            biz_id: biz_id_map[biz_source],
            biz_operator: biz_operator,
            biz_source: biz_source,
            biz_user_id: uid,
            biz_status: 'INIT',
            pay_type: 'ALIPAY',
            pay_return_url: websvr + '/sdkpack/sdktarot3/payment.html?biz_source=' + biz_source + '&biz_back=true',
            pay_status: '',
            biz_type: 'Senior',
            gen_preview: 0,
            flow_type: 'INF_PAY_GEN',
            op_from: op_from,

            biz_user_name: '塔罗2',
            biz_user_sex: formData.sex,
            biz_user_phone: user_phone,
            biz_user_birth_location: formData.status,
            biz_user_live_location: formData.card,

            biz_user_birth_location2: 1
        }
    };
    // console.log(JSON.stringify(param))
    $.ajax({
        type: 'post',
        url: httpsvr + '/proreport/genOrder.do',
        data: JSON.stringify(param),
        success: function(res) {
            hideLoading();
            var data = res.var,
                tarotArry = res.tarot;
            if (res.code == 'S_OK') {
                LXMBase.setItem('biz_result_id', data.proreport_biz_result_id);
                LXMBase.setObjItem('tarot_data',tarotArry);
                LXMBase.data.proreport_biz_result_id = data.proreport_biz_result_id;

                tiktok(data, tarotArry);
                window.location.replace('payment.html?proreport_biz_result_id=' + data.proreport_biz_result_id);
            } else if (res.code == 'FA_PAYED_NOREPORT' || res.code == 'FA_EXIST') {
                window.location.replace('report.html?proreport_biz_result_id=' + data.proreport_biz_result_id);
            } else if (res.code == 'FA_OP_TOO_FREQ') {
                alertMsg('操作过于频繁，请稍后再试');
            } else {
                alertMsg('系统繁忙，请稍后再试');
            }
        },
        error: function() {
            alertMsg('网络繁忙请稍后再试(GENO_ERROR)');
        },
        complete: function() {
            LXMBase.data.sc_genorder = false;
        }
    });
}

function tiktok(data, dataTarot) {
    var tmpSetData = {},
        tmpGetData = LXMBase.getObjItem('tarot_vdata');
    
    // 设置数据
    tmpSetData.sexTxt = data.biz_user_sex == 1 ? '男' : '女';
    tmpSetData.status = dataTarot[0].married == 1 ? '单身' : '非单身';
    tmpSetData.card = dataTarot[0].tarot_type;

    if (tmpGetData) {
        var valid = true;
        if (tmpSetData.sexTxt != tmpGetData.sexTxt) {
            valid = false;
        }
        if (tmpSetData.status != tmpGetData.status) {
            valid = false;
        }
        if (tmpSetData.card != tmpGetData.card) {
            valid = false;
        }
        
        // console.log(valid);
        // 一样就用原来的时间
        // 不一样就设置新的时间，并且覆盖数据;
        if (valid) {
            // 检查时间是否过期
            var tmpTime = LXMBase.getItem('tarot_time');
            if (tmpTime) {
                var tmpDate = new Date(),
                    nowDate = new Date();
                tmpDate.setTime(tmpTime);
                if (tmpDate.getTime() - nowDate.getTime() <= 0) {
                    setTiktokTime();
                }
            }
        } else {
            LXMBase.setObjItem('tarot_vdata', tmpSetData);
            setTiktokTime();
        }

    } else {
        LXMBase.setObjItem('tarot_vdata', tmpSetData);
        // 本地没有记录，开始倒计时
        setTiktokTime();
    }
}

function setTiktokTime() {
    var tmpMin = 30;    // 统一设置方便测试

    var tmpDate = new Date();
    tmpDate.setMinutes(tmpDate.getMinutes() + tmpMin);
    LXMBase.setItem('tarot_time', tmpDate.getTime());
    return tmpDate;
}